package com.sec.android.app.myfiles.widget.listview;

import android.content.Context;
import android.view.View;

import com.sec.android.app.myfiles.R;
import com.sec.android.app.myfiles.activity.AbsMyFilesActivity;
import com.sec.android.app.myfiles.decorator.tablet.TabletColumnListAdapterDecorator;
import com.sec.android.app.myfiles.facade.MyFilesFacade;
import com.sec.android.app.myfiles.feature.MultiWindowMgr;
import com.sec.android.app.myfiles.fragment.filelist.FileListAdapter;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.navigation.NavigationInfo;
import com.sec.android.app.myfiles.util.PreferenceUtils;
import com.sec.android.app.myfiles.util.UiUtils;
import com.sec.android.app.myfiles.widget.tablet.ColumnListHeaderView;
import com.sec.android.app.myfiles.widget.tablet.MyFilesHorizontalScrollView;

/**
 * Created by jaebae on 2016-01-06.
 */
public class ColumnListViewImp extends AbsListViewImp implements ColumnListHeaderView.ColumnEventListener {
    private final MyFilesHorizontalScrollView mScrollView;
    final ColumnListHeaderView mColumnHeaderView;
    private final View mColumnHeaderBottomLine;
    private final MultiWindowMgr mMultiWindow;
    private TabletColumnListAdapterDecorator mListAdapterDecorator;

    public interface ColumnInfo {
        int NAME = 0;
        int TIME = 1;
        int TYPE = 2;
        int SIZE = 3;
        int MAX = 4;
    }

    @Override
    public void setAdapter(FileListAdapter adapter) {
        mListAdapterDecorator = new TabletColumnListAdapterDecorator(mContext, adapter, mMultiWindow);
        adapter.setDecorator(mListAdapterDecorator);
        super.setAdapter(adapter);
    }

    public ColumnListViewImp(Context context, View parentView, NavigationInfo navigationInfo) {
        super(context, parentView, navigationInfo);
        mScrollView = (MyFilesHorizontalScrollView) parentView.findViewById(R.id.tablet_column_list_scroll);
        mColumnHeaderView = (ColumnListHeaderView) parentView.findViewById(R.id.tablet_column_list_header);
        mColumnHeaderBottomLine = parentView.findViewById(R.id.column_header_bottom_line);
        mColumnHeaderView.setColumnSizeChangedListener(this);
        mMultiWindow = MultiWindowMgr.getInstance((AbsMyFilesActivity) context, ((AbsMyFilesActivity) context).getProcessId());
    }

    @Override
    protected int _getListLayoutId() {
        return R.id.tablet_column_list;
    }

    @Override
    protected int _getItemLayoutId() {
        if (mNavigationInfo != null && mNavigationInfo.getStorageType() == FileRecord.StorageType.Category) {
            FileRecord record = mNavigationInfo.getCurRecord();
            if (UiUtils.isCategoryFolderList(record)) {
            /* only when 1 depth */
                return R.layout.file_column_category_folder_list_item;
            }
        }
        return R.layout.file_column_list_item;
    }

    @Override
    protected int _getViewStubId() {
        return R.id.column_list_stub;
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(visibility);
        mScrollView.setVisibility(visibility);
        mColumnHeaderView.setVisibility(visibility);
        mColumnHeaderBottomLine.setVisibility(visibility);
    }

    @Override
    public void onColumnSizeChanged(int nColumnIndex, int nWidth) {
        mListAdapterDecorator.setColumnWidth(nColumnIndex, nWidth);
        invalidateViews();
    }

    @Override
    public void onColumnSizeChangedFinished(int nColumnIndex, int nWidth) {
        PreferenceUtils.setColumnListWidth(mContext, mMultiWindow.getWindowState(), nColumnIndex, nWidth);
    }

    @Override
    public void onSortByChanged(int type) {
        MyFilesFacade.setSortBy(mFragment.getProcessId(), mFragment, type);
        if (mFragment.isSelectActionMode() || (mNavigationInfo != null && mNavigationInfo.isPickerMode())) {
            mFragment.selectAll(false);
        }
    }

    @Override
    protected void _refreshLayout() {
        for (int i = 0; i < ColumnInfo.MAX; i++) {
            int width = PreferenceUtils.getColumnListWidth(mContext, mMultiWindow.getWindowState(), i);
            width = mColumnHeaderView.setColumnWidth(i, width);
            mListAdapterDecorator.setColumnWidth(i, width);
        }
    }

    @Override
    public void onLongPressDragStarted(int startX, int startY) {
        if (mScrollView != null) {
            mScrollView.setScrollable(false);
        }
    }

    @Override
    public void onLongPressDragEnded(int endX, int endY) {
        if (mScrollView != null) {
            mScrollView.setScrollable(true);
        }
    }
}
